import unittest
import requests


class OpenpyxlTest(unittest.TestCase):

    # 单纯的创建xlsx文件
    def test_get_api(self):
        url = "https://api.github.com/events"
        res = requests.get(url)
        print(res)
        # 查看返回值类型
        print(type(res))
        # 查看接口请求的状态, 例如200标识成功, 404标识没有
        print(res.status_code)
        # 获取接口返回的json数据
        print(res.json())
        # 获取响应的字符串格式
        print("text >>> " + res.text)

    def test_post_api(self):
        url = "http://httpbin.org/post";
        res = requests.post(url, data={"id": 12})
        print(res)
        print(res.json())

    def test_get_api2(self):
        url = "http://httpbin.org/get";
        params = {
            "key1": "value1",
            "key2": ["v1", "v2"]
        }
        # 这种方式请求时会自动将参数拼接到url后面
        res = requests.get(url, params=params)
        print(res)
        print(res.url)

    # 设置header, 模拟浏览器
    def test_request_set_header(self):
        new_headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36"
        }
        response = requests.get("https://www.zhihu.com", headers=new_headers)
        print(response.text)  # 正常输出


if __name__ == '__main__':
    unittest.main()
